package cn.samler.shopapi.Controller;

import cn.samler.shopapi.Entity.User;
import cn.samler.shopapi.Model.Request.Order.CreateByCart;
import cn.samler.shopapi.Model.Request.Order.Goods;
import cn.samler.shopapi.Model.Request.Order.Payment;
import cn.samler.shopapi.Model.Response.Order.OrderDetails;
import cn.samler.shopapi.Model.Sys.Response;
import cn.samler.shopapi.Model.Sys.ServiceResult;
import cn.samler.shopapi.Service.OrderService;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/order")
public class OrderController {
    private final OrderService orderService;

    OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    /**
     * 获取订单列表
     *
     * @param request
     * @param page
     * @return
     */
    @GetMapping("list")
    public Response list(HttpServletRequest request, Integer type, Integer page, Integer offset) {
        if (page == null || page <= 0) page = 1;
        if (offset == null || offset < 10) offset = 10;

        return Response.success(orderService.listOrder((User) request.getAttribute("current_user"), type, page, offset));
    }

    /**
     * 获取订单详情
     *
     * @param request
     * @param id
     * @return
     */
    @GetMapping("details")
    public Response details(HttpServletRequest request, String id) {
        OrderDetails order = orderService.findById((User) request.getAttribute("current_user"), id);
        if (order != null)
            return Response.success(order);

        return Response.failure("没有找到对应的订单");
    }

    /**
     * 购买单品
     *
     * @param request
     * @param goods
     * @return
     */
    @PostMapping("buy")
    public Response buy(HttpServletRequest request, @RequestBody Goods goods) {
        ServiceResult result = orderService.createByGoods((User) request.getAttribute("current_user"), goods);

        return result.isSuccess() ? Response.success(result.getData()) : Response.failure(result.getData());
    }

    /**
     * 购物车结算
     *
     * @param request
     * @param cart
     * @return
     */
    @PostMapping("settlement")
    public Response settlementByCart(HttpServletRequest request, @RequestBody CreateByCart cart) {
        ServiceResult result = orderService.createByCart((User) request.getAttribute("current_user"), cart);

        return result.isSuccess() ? Response.success(result.getData()) : Response.failure(result.getData());
    }

    /**
     * 订单付款
     *
     * @param request
     * @param payment
     * @return
     */
    @PostMapping("payment")
    public Response payment(HttpServletRequest request, @RequestBody Payment payment) {
        if (orderService.payment((User) request.getAttribute("current_user"), payment))
            return Response.success();

        return Response.failure("付款失败");
    }

    /**
     * 确认收货
     *
     * @param request
     * @param id
     * @return
     */
    @GetMapping("confirm")
    public Response confirm(HttpServletRequest request, String id) {
        if (orderService.confirmOrder((User) request.getAttribute("current_user"), id))
            return Response.success();

        return Response.failure("确认失败");
    }
}
